﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFP;
using SFP.CollisionDetection;
using HamsterDelivery.LevelStructure.Reactions;

namespace HamsterDelivery.LevelStructure.GameObjects
{
    public class LetterObject : GameObject
    {
        private JumpingLetterReaction JumpingLetterReaction { get; set; }

        public double VelocityX { set; get; }
        public double VelocityY { set; get; }
        public bool Jumping { set; get; }

        HamsterGame game;
        private Random ranDirection;

        public LetterObject(HamsterGame game)
        {
            this.game = game;
            ranDirection = new Random();
            Jumping = false;
            JumpingLetterReaction = new JumpingLetterReaction(this);
        }

        public void startJump()
        {
            Jumping = true;
            int value = ranDirection.Next(0,2);
            VelocityX = value == 0 ? 150 : -150;
            VelocityY = 350;
        }

        public void update(double time)
        {
            if (!Jumping)
                return;

            VelocityY -= Level.GRAVITY * time / 1000;

            X += VelocityX * time / 1000;
            Y -= VelocityY * time / 1000;

            processLevelCollisions();
        }

        public void processLevelCollisions()
        {
            Collision.clean();
            long streetSetID = Collision.collisionsForGameObjects(game.Level.StreetBlocks, GetRectangle(), true);
            Collision.processFilters(vMoveDirection(), hMoveDirection());
            JumpingLetterReaction.reactToCollision(new long[] { streetSetID });
        }

        public Direction vMoveDirection()
        {
            if (VelocityY > 0) return Direction.Up;
            else if (VelocityY < 0) return Direction.Down;

            return Direction.None;
        }

        public Direction hMoveDirection()
        {
            if (VelocityX < 0) return Direction.Left;
            else if (VelocityX > 0) return Direction.Right;

            return Direction.None;
        }
    }
}
